﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Freecart_Local_.Classes
{
    public class CartItemsCollector
    {
        private DataTable _itemsTable;
        public DataTable Table { get { return this._itemsTable; } }

        /// <summary>
        /// This method Adds a CartRow object to Cart by adding a new row to datatable
        /// </summary>
        public bool Add(CartRow Row)
        {
            bool _modified = false;

            try
            {
                DataRow[] foundRow = this._itemsTable.Select("ID = " + Row.ID);

                // check if row exists, increment row's qantity with Row.Quantity
                if (foundRow.Length > 0)
                {
                    for (int i = 0; i < foundRow.Length; i++)
                    {
                        foundRow[i].BeginEdit();
                        foundRow[i]["Quantity"] = Int32.Parse(foundRow[i]["Quantity"].ToString()) + Row.Quantity;
                        foundRow[i].EndEdit();
                    }

                    _modified = true;
                }
                else
                {
                    // add new row
                    this._itemsTable.Rows.Add(Row.ID, Row.Quantity, Row.Comment, Row.Price);

                    _modified = true;
                }
            }
            catch
            {
                throw;
            }

            if (_modified == true)
                this._itemsTable.AcceptChanges();

            return _modified;
        }

        /// <summary>
        /// This method Removes a row within the cart instance
        /// </summary>
        public void Remove(int RowId)
        {

        }

        public CartItemsCollector()
        {
            // constructor
            this._itemsTable = new DataTable();
        }
    }
}
